#!/usr/bin/python
#-*- coding:utf-8 -*-
# Author : yh_zhu
# Description: extract module definition from <file> 
#     Usage : gen_bbox.py <file>
#             
#
################################################################
import re, os

module     = re.compile(r'\s*module\s+.*')
end_module = re.compile(r'\s*endmodule\s+.*')
comma      = re.compile(r'.*\)\s*;\s*')

def bbox_from_file(fname) :
    nesting_level = 0
    wait_comma = False
    found_comma = True
    with open(fname) as f:
        for line in f:
            if re.match(module, line):
                nesting_level +=1
                if nesting_level == 1:
                    wait_comma  = True
                    found_comma = False
                    yield line
            elif re.match(end_module, line):
                nesting_level -=1
                yield line
            else: # neither 'module' or 'endmodule'
                if nesting_level == 0 :
                    yield line
                elif re.match(comma, line) :
                    if not found_comma : yield line
                    wait_comma  = False
                    found_comma = True
                elif wait_comma and not found_comma :
                    yield line


if __name__ == '__main__':
    import sys
    print ''.join(bbox_from_file(sys.argv[1]))

